﻿using System;
using System.Collections;
using UnityEngine;
using Random = UnityEngine.Random;

public class Role : MonoBehaviour
{
    public int HP;
    public int MP;
    public float Speed = 5f;

    public Action OnDead;

    public void Init()
    {
        HP = 100;
    }

    public void ReduceHP(int value)
    {
        HP = Mathf.Max(HP - value, 0);
        if (HP <= 0)
        {
            OnDead?.Invoke();
        }
    }

    private bool m_IsMoveEnd =true ;
    private Vector3 m_TargetDir ;
    private Vector3 m_TargetPos;

    private Vector3 TempPos;
    private void Update()
    {
        Debug.DrawLine(m_TargetPos, TempPos, Color.red);
        Debug.DrawLine(m_TargetPos, transform.position,Color.blue);
    }

    public void Move(Bounds bounds)
    {
        if (m_IsMoveEnd)
        {
            TempPos = transform.position;
            m_IsMoveEnd = false;
            Vector3 randomPoint = bounds.center + Random.insideUnitSphere *Random.Range(1,50);
            randomPoint.y = 0;
            m_TargetPos = bounds.ClosestPoint(randomPoint);
            transform.LookAt(m_TargetPos);
            m_TargetDir = (m_TargetPos - transform.position).normalized;
        }

        transform.position +=  m_TargetDir * Time.deltaTime * Speed;
        Debug.Log(Vector3.Dot(m_TargetDir, (m_TargetPos - transform.position).normalized));
        if (Vector3.Dot(m_TargetDir, m_TargetPos - transform.position) <= 0)
        {
            m_IsMoveEnd = true;
        }
    }
}

